// pages/EditProject/EditProject.js
const axios = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maxLength: 200,
    monny: 100,
    project: {
      pro_id: 0,
      user_id: 0,
      pro_name: "",
      pro_desc: "",
      pro_imgs: [],
      deadline: "",
      pre_reward: 0,
      real_reward: 0,
      pro_status: 1,
      share_count: "0",
      read_count: "0",
      attention_count: 0,
      meshapenum: 100,
    }
  },

  /**
   * 项目详情
   */
  projectDetailsHttp: function(proId) {
    var that = this;
    var parame = {
      pro_id: proId
    }
    axios.POST(true, 'Orders/order_detail', parame, function(res) {
      var images = res.pro_imgs.split(',');
      var projectMessage = res;
      console.log(images.length)
      projectMessage.pro_imgs = images
      that.setData({
        project: projectMessage
      })
    });
  },

  countMonitor: function(e) {
    var projectnew = this.data.project;
    projectnew.pro_desc = e.detail.value;
    this.setData({
      project: projectnew
    })
  },

  chooseImage: function(e) {
    var projectnew = this.data.project;
    var that = this;
    wx.chooseImage({
      count: 4,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        for (let i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: axios.urlR.baseUrl + axios.urlR.upLoad, //真实的接口地址
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {},
            success: function (upres) {
              var _data = JSON.parse(upres.data)
              if (_data.code == 200) {
                projectnew.pro_imgs.push(_data.path)
                  that.setData({ 
                    project: projectnew
                    })
              } else {
                axios.model('数据获取失败', _data.msg, false, undefined, undefined, function () {

                })
              }
            },
            complete: function (r) {
            }
          })
        }

        // projectnew.pro_imgs.push(res.tempFilePaths)
        // that.setData({
          // project: projectnew
        // });
      }
    })
  },
  previewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.project.pro_imgs // 需要预览的图片http链接列表
    })
  },

  inputMnChange: function(e) {
    var projectnew = this.data.project;
    projectnew.pre_reward = e.detail.value;
    this.setData({
      project: projectnew
    })
  },

  inputTitleChange: function(e) {
    var projectnew = this.data.project;
    projectnew.pro_name = e.detail.value;
    this.setData({
      project: projectnew
    })
  },

  editProjectClick: function() {
    if (null == this.data.project.pro_name || this.data.project.pro_name.length == 0) {
      axios.ti("标题不可为空", "none");
      return;
    }

    if (null == this.data.project.pre_reward || this.data.project.pre_reward == 0) {
      axios.ti("转发赏金不可为空在为0", "none");
      return;
    }
    var proId = this.data.project.pro_id;
    var that = this;
    var parame = {
      pro_id: proId,
      pro_name: this.data.project.pro_name,
      pro_desc: this.data.project.pro_desc,
      pro_imgs: this.data.project.pro_imgs.join(','),
      deadline: this.data.project.deadline,
      pre_reward: this.data.project.pre_reward
    }
    console.log(parame)
    axios.POST(true, 'Orders/order_edit', parame, function(res) {
      axios.ti("成功", "success");
      wx.navigateBack({

      })
    });
  },
  deleteimg: function(v) {
    var projectnew = this.data.project;
    var index = v.currentTarget.dataset.position;
    projectnew.pro_imgs.splice(index, 1);
    this.setData({
      project: projectnew
    });
  },
  bindDateChange: function(e) {
    var projectnew = this.data.project;
    projectnew.deadline = e.detail.value;
    this.setData({
      project: projectnew
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var pId = options.pId;
    console.log(pId);
    this.projectDetailsHttp(pId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})